package com.yuexun.beilunpatient.ui.doctor.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.ui.doctor.bean.CollectionDocInfoBean;
import com.yuexun.beilunpatient.ui.doctor.bean.SeeDocInfoBean;
import com.yuexun.beilunpatient.ui.doctor.bean.SignDocInfoBean;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.Collection;

/**
 * Created by chenss on 2018/3/22 0022.
 */

public class DoctorListAdapter extends KJAdapter<Object> {

    ICallBackListener listener;

    public void setListener(ICallBackListener listener) {
        this.listener = listener;
    }

    //itemA类的type标志
    private static final int TYPE_A = 0;
    //itemB类的type标志
    private static final int TYPE_B = 1;
    //itemC类的type标志
    private static final int TYPE_C = 2;

    public DoctorListAdapter(AbsListView view, Collection<Object> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder helper, Object item, boolean isScrolling, final int position) {
        super.convert(helper, item, isScrolling, position);

        int type = getItemViewType(item);
        TextView tv_collect = (TextView) helper.getView(R.id.tv_collect);
        tv_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                listener.callback(bundle);
            }
        });

        switch (type) {
            case TYPE_A:
                SignDocInfoBean signDocInfoBean = (SignDocInfoBean) item;
                helper.setText(R.id.doctor_name,signDocInfoBean.getDoct_name());
                helper.setText(R.id.doctor_hospital,signDocInfoBean.getHosp_name());
                helper.setText(R.id.doctor_dep,signDocInfoBean.getDept_name());
                if(signDocInfoBean.isColletion()){
                    tv_collect.setText("取消收藏");
                }else{
                    tv_collect.setText("收藏");
                }
                break;

            case TYPE_B:
                SeeDocInfoBean seeDocInfoBean = (SeeDocInfoBean) item;
                helper.setText(R.id.doctor_name,seeDocInfoBean.getCli_doct_name());
                helper.setText(R.id.doctor_hospital,seeDocInfoBean.getHosp_name());
                helper.setText(R.id.doctor_dep,seeDocInfoBean.getCli_dept_name());
                if(seeDocInfoBean.isColletion()){
                    tv_collect.setText("取消收藏");
                }else{
                    tv_collect.setText("收藏");
                }
                break;

            case TYPE_C:
                CollectionDocInfoBean collectionDocInfoBean = (CollectionDocInfoBean) item;
                helper.setText(R.id.doctor_name,collectionDocInfoBean.getUserName());
                helper.setText(R.id.doctor_hospital,collectionDocInfoBean.getHospitalName());
                helper.setText(R.id.doctor_dep,"");
                tv_collect.setText("取消收藏");
                break;
        }
    }

    /**
     * 获得itemView的type
     * @param object
     * @return
     */
    public int getItemViewType(Object object) {
        int result = 0;
        if (object instanceof SignDocInfoBean) {
            result = TYPE_A;
        }
        else if (object instanceof SeeDocInfoBean) {
            result = TYPE_B;
        }
        else if (object instanceof CollectionDocInfoBean) {
            result = TYPE_C;
        }
        return result;
    }
}
